package cn.huanzi.qch.baseadmin.travel.thotel.service;

import cn.huanzi.qch.baseadmin.common.service.*;
import cn.huanzi.qch.baseadmin.travel.thotel.pojo.THotel;
import cn.huanzi.qch.baseadmin.travel.thotel.vo.THotelVo;
import cn.huanzi.qch.baseadmin.travel.thotel.repository.THotelRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class THotelServiceImpl extends CommonServiceImpl<THotelVo, THotel, Integer> implements THotelService{

    @PersistenceContext
    private EntityManager em;
    @Autowired
    private THotelRepository tHotelRepository;


    // TODO: 查询数据返回问题
    @Override
    public List<THotelVo> findAll() {
        List<THotel> allHotel = tHotelRepository.findAll();
        ArrayList<THotelVo> tHotels = new ArrayList<>();
        for (THotel tHotel : allHotel) {
            THotelVo tHotelVo = new THotelVo();
            BeanUtils.copyProperties(tHotel, tHotelVo);
            tHotels.add(tHotelVo);
        }
        return tHotels;
    }

    @Override
    public THotel findByHotelID(Integer id) {
        Optional<THotel> hotelById = tHotelRepository.findById(id);
        return hotelById.get();
    }
}
